package com.zhima.data;

import cn.hutool.core.util.RandomUtil;

import java.math.BigDecimal;

/**
 * 数组
 */
public class ArrayRandomData extends AbstractRandomData {
    private final int minLength;
    private final int maxLength;
    private final int twoDimensionMinLength;
    private final int twoDimensionMaxLength;
    private ArrayDimension dimension;

    private RandomDataType elementRandomDataType;

    public ArrayRandomData(
            BigDecimal min,
            BigDecimal max,
            int minLength,
            int maxLength,
            int twoDimensionMinLength,
            int twoDimensionMaxLength,
            ArrayDimension dimension,
            RandomDataType elementRandomDataType) {
        super(min, max);
        this.minLength = minLength;
        this.maxLength = maxLength;
        this.twoDimensionMinLength = twoDimensionMinLength;
        this.twoDimensionMaxLength = twoDimensionMaxLength;
        this.dimension = dimension;
        this.elementRandomDataType = elementRandomDataType;
    }

    /**
     * 返回的是数组元素的类型
     *
     * @return
     */
    @Override
    public RandomDataType getRandomDataType() {
        return RandomDataType.ARRAY;
    }

    @Override
    public RandomDataType getElementDataType() {
        return elementRandomDataType;
    }

    @Override
    public Object randomElement() {
        switch (dimension) {
            case ONE: {
                Object[] ans = new Object[RandomUtil.randomInt(minLength, maxLength + 1)];
                for (int i = 0; i < ans.length; i++) {
                    ans[i] = super.randomElement();
                }
                return ans;
            }
            case TWO: {
                Object[][] ans = new Object[RandomUtil.randomInt(minLength, maxLength + 1)][RandomUtil.randomInt(twoDimensionMinLength, twoDimensionMaxLength + 1)];
                for (int i = 0; i < ans.length; i++) {
                    for (int j = 0; j < ans[i].length; j++) {
                        ans[i][j] = super.randomElement();
                    }
                }
                return ans;
            }
            default:
                throw new IllegalArgumentException("暂只支持生成一维和二维数组");
        }
    }
}
